﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.UI.Bovespa.Contabil.Presenter;

namespace Sinacor.UI.Bovespa.Contabil.View.SmartClient
{
    /// <summary>
    /// Interaction logic for PesquisaContasContabeis.xaml
    /// </summary>
    public partial class PesquisaContasContabeis : SinacorWindow, IPesquisaContasContabeis
    {
        #region Presenter

        /// <summary>
        /// Propriedade com a presenter da view
        /// </summary>
        public Presenter.PesquisaContasContabeis pesquisaContasContabeisPresenter { get; set; }

        #endregion

        #region Construtor

        public PesquisaContasContabeis()
        {
            // Este deve ser o primeiro método do contrutor da View
            InitializeComponent();

            // Inicialize a presenter no construtor
            this.pesquisaContasContabeisPresenter = new Presenter.PesquisaContasContabeis(this);            

            // Configure a presenter no DataContext da janela (para facilitar os bindings)
            this.DataContext = this.pesquisaContasContabeisPresenter;

            this.dtgContasContabeis.NewItemType = typeof(Model.ContaContabil);

            this.AfterInitialize += new RoutedEventHandler(PesquisaContasContabeis_AfterInitialize);
        }

        public PesquisaContasContabeis(int? codigoResumido, string numeroConta, string nomeConta)
        {
            // Este deve ser o primeiro método do contrutor da View
            InitializeComponent();

            // Inicialize a presenter no construtor
            this.pesquisaContasContabeisPresenter = new Presenter.PesquisaContasContabeis(this);
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.CodigoResumido = codigoResumido;
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.NomeConta = nomeConta;
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.NumeroConta = numeroConta;

            // Configure a presenter no DataContext da janela (para facilitar os bindings)
            this.DataContext = this.pesquisaContasContabeisPresenter;

            this.dtgContasContabeis.NewItemType = typeof(Model.ContaContabil);

            this.AfterInitialize += new RoutedEventHandler(PesquisaContasContabeis_AfterInitialize);
        }

        #endregion

        #region AfterInitialize

        /// <summary>
        /// Event Handler disparado após a inicialização dos controles
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PesquisaContasContabeis_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();
            this.pesquisaContasContabeisPresenter.Inicializar();
            this.HideUserWaitingProcess();
        }

        #endregion

        #region IPesquisaContasContabeis Members

        public void AtualizarContaContabil()
        {
            dtgContasContabeis.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        #endregion        

        #region Eventos

        private void btnPesquisar_Click(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();

            //Forçar binding
            this.MoveFocus(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.Next));
            this.MoveFocus(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.Previous));

            this.pesquisaContasContabeisPresenter.Pesquisar();
            this.HideUserWaitingProcess();
        }

        private void dtgContasContabeis_ConfirmSelection(object sender, ItemEventArgs e)
        {
            if (this.dtgContasContabeis.SelectedItems.Count > 0)
            {
                this.pesquisaContasContabeisPresenter.ContaContabilSelecionada = ((Model.ContaContabil)dtgContasContabeis.Items.CurrentItem);
                this.Close();
            }
            e.Handled = true;
        }

        private void btnNovaPesquisa_Click(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.CodigoResumido = null;
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.NomeConta = string.Empty;
            this.pesquisaContasContabeisPresenter.FiltroPesquisa.NumeroConta = string.Empty;
            this.txtCodigoResumido.UpdateTarget(SinacorInt32TextBox.ValueProperty);
            this.txtNomeConta.UpdateTarget(SinacorTextBox.TextProperty);
            this.txtNumeroConta.UpdateTarget(SinacorTextBox.TextProperty);
            this.HideUserWaitingProcess();
        }

        #endregion
    }
}